import java.util.Arrays;

public class TestHeap {

    public int[] elem;
    public int usedSize;


    public TestHeap(){
        elem = new int[10];
    }

    public void initElem(int[] array){
        for (int i = 0; i < array.length; i++) {
            this.elem[i] = array[i];
            this.usedSize++;
        }
    }

    public void createHeap(){
        //先找出最后一个树的父节点下标，从最后一棵树开始创建堆
        //最后一个树的父节点的下标: (usedSize-1-1)/2
        for (int parent=(usedSize-1-1)/2;parent>=0;parent--){
            //一个parent代表调整一棵树
            shiftDown(parent,this.usedSize);
        }
    }

    //堆排
    public void sort(){
        int end = usedSize-1;
        while (end>0){
            swap(elem,0,end);
            shiftDown(0,end);
            end--;
        }
    }

    private void shiftDown(int parent, int usedSize) {
        int child = parent*2+1;
        while (child<usedSize){
            //先找出左右子树的最大值
            if (child+1<usedSize && elem[child+1]>elem[child]){
                child=child+1;
            }
            if (elem[child]>elem[parent]){
                swap(elem,child,parent);
                //交换完，继续向下调整
                parent=child;
                child=parent*2+1;
            } else {
                break;
            }
        }
    }

    //向堆中插入元素
    public void push(int val){
        //判满
        if (isFull(this.elem)){
            elem = Arrays.copyOf(elem,2*elem.length);
        }
        elem[usedSize]=val;
        //向上调整
        shiftUp(usedSize);
        usedSize++;
    }

    private void shiftUp(int usedSize) {
        int child = usedSize;
        int parent = (usedSize-1)/2;
        while (parent>=0){
            if (elem[child]>elem[parent]){
                swap(elem,child,parent);
                child=parent;
                parent=(child-1)/2;
            } else {
                break;
            }
        }
    }

    private void swap(int[] elem, int child, int parent) {
        int tmp = elem[parent];
        elem[parent]=elem[child];
        elem[child]=tmp;
    }


    public int poll(){
        if (isEmpty()){
            throw new RuntimeException("Heap is Empty");
        }
        int val = elem[0];
        swap(elem,0,usedSize-1);
        shiftDown(0,usedSize-1);
        return val;
    }

    public int peek(){
        if (isEmpty()){
            throw new RuntimeException("Heap is Empty");
        }
        return elem[0];
    }

    private boolean isEmpty() {
        return usedSize==0;
    }


//    public int poll(){
//        if (!isEmpty(this.elem)){
//            int val=elem[0];
//            swap(elem,0,usedSize-1);
//            usedSize--;
//            shiftDown(0,usedSize);
//        }else {
//            throw new RuntimeException("堆为空");
//        }
//        return elem[usedSize];
//    }
//
//    private boolean isEmpty(int[] elem) {
//        return usedSize==0;
//    }




    public boolean isFull(int[] elem){
        return usedSize==elem.length;
    }



    private void shiftDownTest(int parent, int usedSize) {
        int leftChild = parent*2+1;
        int rightChild = 0;
        while (leftChild<usedSize){
            if(leftChild+1 < usedSize && elem[leftChild] < elem[leftChild+1]){
                rightChild = leftChild+1;
            }
            int rightValue = elem[rightChild];
            int leftValue = elem[leftChild];
            int changeIndex = rightValue>=leftValue ? rightChild : leftChild;
            int max = Math.max(rightValue,leftValue);
            if (max>elem[parent]){
                int tmp = elem[parent];
                elem[parent]=max;
                elem[changeIndex]=tmp;
                parent=changeIndex;
                leftChild=parent*2+1;
            }else{
                break;
            }
        }
    }


    //    public void push(int val){
//        //判断是否满了
//        if (isFull(this.elem)){
//            elem = Arrays.copyOf(elem,2*elem.length);
//        }
//        elem[usedSize]=val;
//        shiftUp(usedSize);
//        usedSize++;
//    }


    //    private void shiftUp(int child) {
//        int parent = (child-1)/2;
//        while (parent>=0){
//            if (elem[child]>elem[parent]){
//                swap(elem,child,parent);
//                child=parent;
//                parent=(child-1)/2;
//            }else {
//                break;
//            }
//        }
//    }

}
